package com.woniu.zsd.platform.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.zsd.common.util.JWTUtil;
import com.woniu.zsd.common.util.RedisKey;
import com.woniu.zsd.common.util.Result;
import com.woniu.zsd.common.web.PageInfo;
import com.woniu.zsd.platform.annotation.RequirePermission;
import com.woniu.zsd.platform.annotation.RequireRole;
import com.woniu.zsd.platform.dto.AccountDto;
import com.woniu.zsd.platform.form.AccountForm;
import com.woniu.zsd.platform.form.AddAccountForm;
import com.woniu.zsd.platform.form.ChatForm;
import com.woniu.zsd.platform.form.SelectAccountForm;
import com.woniu.zsd.platform.model.Account;
import com.woniu.zsd.platform.param.AccountParam;
import com.woniu.zsd.platform.param.AddAccountParam;
import com.woniu.zsd.platform.param.ChatParam;
import com.woniu.zsd.platform.param.SelectAccountParam;
import com.woniu.zsd.platform.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangtao
 * @since 2025年02月18日
 */
@RestController("platformAccountController")
@RequestMapping("/account")
@Api(tags = "管理员类接口")
public class AccountController {
    @Resource
    private  JWTUtil jwtUtil;
    @Resource
    private AccountService accountService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 登录接口
     * @param form 手机号 密码
     * @return 返回登录成功的用户信息
     * @throws Exception
     */
    @PostMapping("/accountLogin")
    @ApiOperation("后台登录接口")
    @RequireRole({"超级管理员"})
    @RequirePermission("后台登录")
    public Result accountLogin(@RequestBody AccountForm form, HttpServletResponse response) throws Exception {
        System.out.println(form.getPhone());
        System.out.println(form.getPassword());
        AccountDto dto = accountService.AccountLogin(BeanUtil.toBean(form, AccountParam.class));
        //生成令牌
        Map<String, Object> body = BeanUtil.beanToMap(dto);
        String accessToken = jwtUtil.createToken(body, 30);
        String refreshToken = jwtUtil.createToken(body, 3*24*60);
        //通过响应头将jwt返回给客户端
        response.setHeader("token", accessToken);
        //在跨域请求访问时，必须公开该请求头,前端才能获取到token
        response.setHeader("Access-Control-Expose-Headers", "token");
        //生成长短token
        stringRedisTemplate.opsForValue().set(RedisKey.ACCESS_EXPIRE_TOKEN2(accessToken),"",30, TimeUnit.MINUTES);
        stringRedisTemplate.opsForValue().set(RedisKey.ACCESS_REFRESH_TOKEN2(accessToken),refreshToken,3, TimeUnit.DAYS);
        return Result.success("登录成功",dto);
    }

    /**
     * 新增管理员接口
     * @param form 管理员信息
     * @return 返回新增管理员成功
     * @throws Exception
     * */
    @PostMapping("/AddAccount")
    @RequireRole({"超级管理员"})
    @RequirePermission("新增后台人员")
    @ApiOperation("新增后台人员接口")
    public Result AddAccount(@RequestBody AddAccountForm form) throws Exception {
        accountService.AddAccount(BeanUtil.toBean(form, AddAccountParam.class));
        return Result.success("新增后台人员成功");
    }


    //查询所有后端人员
    @GetMapping("/selectAccount")
    @RequireRole({"超级管理员"})
    @RequirePermission("查询后端人员")
    @ApiOperation("查询后端人员接口")
    public Result selectAccount(SelectAccountForm form)throws Exception {
        PageInfo pageInfo  = accountService.selectAccount(BeanUtil.toBean(form, SelectAccountParam.class));
        return Result.success("查询后台人员成功",pageInfo);
    }

    @GetMapping("/selectAccountNoRole")
    @ApiOperation(value = "查询未分配的人员")
    @RequireRole({"超级管理员"})
    @RequirePermission("查询未分配的人员")
    public Result selectAccountNoRole() throws Exception {
        //返回为分配人员的名称
        List<String> list = accountService.selectAccountNoRole();
        return Result.success("消息发送成功",list);
    }


    @PostMapping("/chat")
    @ApiOperation(value = "客户与客服聊天")
    public Result chat(@RequestBody ChatForm form){
        System.out.println("聊天内容 " + form.toString());
        accountService.chat(BeanUtil.toBean(form, ChatParam.class));
        return Result.success("消息发送成功");
    }

    @PostMapping("/chatWithCustomer")
    @ApiOperation(value = "客服与客户聊天")
    public Result chatWithCustomer(@RequestBody ChatForm form){
        System.out.println("聊天内容 " + form.toString());
        accountService.chatWithCustomer(BeanUtil.toBean(form, ChatParam.class));
        return Result.success("消息发送成功");
    }

    @GetMapping("/selectCustomerServiceChatHistory")
    @ApiOperation(value = "查询客服与客户聊天历史记录")
    public Result selectCustomerServiceChatHistory(@RequestParam("customerId") Integer userId){
        return Result.success("查询成功",accountService.selectCustomerServiceChatHistory(userId));
    }


}

